package com.yinsin.utils;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class XmlUtils {

	/*public static void main(String[] args) {
		try {
			XmlNode node = parseXml("<msg><op id='5'><username>yinxin3326</username></op></msg>");
			System.out.println(node);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}*/

	/**
	 * 解析xml字符串
	 * @param xml [String]
	 * @return com.yinsin.utils.XmlUtils.XmlNode
	 * @throws Exception
	 */
	public static XmlNode parseXml(String xml) throws Exception {
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		DocumentBuilder builder = factory.newDocumentBuilder();
		ByteArrayInputStream tInputStringStream = new ByteArrayInputStream(xml.getBytes());
		Document doc = builder.parse(tInputStringStream);
		Element root = doc.getDocumentElement();
		return parse(root);
	}

	/**
	 * 解析xml文件
	 * @param path [String] xml文件路径
	 * @return com.yinsin.utils.XmlUtils.XmlNode
	 * @throws Exception
	 */
	public static XmlNode parse(String path) throws Exception {
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		DocumentBuilder builder = factory.newDocumentBuilder();

		Document doc = builder.parse(new File(path));
		Element root = doc.getDocumentElement();
		return parse(root);
	}

	private static XmlNode parse(Element element) {
		XmlNode xmlNode = new XmlNode();
		String nodeName = element.getNodeName();
		xmlNode.setNodeName(nodeName);
		Map<String, String> attrMap = parseAttr(element);
		xmlNode.setAttr(attrMap);
		NodeList nodeList = element.getChildNodes();
		if (nodeList != null) {
			int length = nodeList.getLength();
			for (int i = 0; i < length; i++) {
				Node node = nodeList.item(i);
				short nodeType = node.getNodeType();
				if (nodeType == Node.ELEMENT_NODE) {
					// 递归
					xmlNode.childs.add(parse((Element) node));
				} else if (nodeType == Node.TEXT_NODE) {
					// 这里会读出\n\t,所以打印不用换行
					xmlNode.setNodeText(node.getNodeValue());
				} else if (nodeType == Node.COMMENT_NODE) {
					xmlNode.setNodeText(node.getNodeValue());
				}
			}
		}
		return xmlNode;
	}

	private static Map<String, String> parseAttr(Element element) {
		Map<String, String> attrs = new HashMap<String, String>();
		NamedNodeMap attrMap = element.getAttributes();
		if (attrMap != null && attrMap.getLength() > 0) {
			int length = attrMap.getLength();
			for (int i = 0; i < length; i++) {
				Node attr = attrMap.item(i);
				String attrName = attr.getNodeName();
				String attrValue = attr.getNodeValue();
				attrs.put(attrName, attrValue);
			}
		}
		return attrs;
	}

	public static class XmlNode {
		private String nodeName;
		private String nodeText;
		private Map<String, String> attr = new HashMap<String, String>();
		private List<XmlNode> childs = new ArrayList<XmlNode>();

		public String getNodeName() {
			return nodeName;
		}

		public void setNodeName(String nodeName) {
			this.nodeName = nodeName;
		}

		public String getNodeText() {
			return nodeText;
		}

		public void setNodeText(String nodeText) {
			this.nodeText = nodeText;
		}

		public Map<String, String> getAttr() {
			return attr;
		}

		public void setAttr(Map<String, String> attr) {
			this.attr = attr;
		}

		public List<XmlNode> getChilds() {
			return childs;
		}

		public void setChilds(List<XmlNode> childs) {
			this.childs = childs;
		}
	}
}
